
load('summary_data/fig1.rda')

x_l = c('[0,90)','[90,95)','[95,99)','[99,99.5)','[99.5,99.9)', '[99.9,)')

shapeify <- function(x) {
  x %>%
    filter(!str_detect(variable, '^N')) %>%
    mutate(
      variable = variable %>%
        forcats::fct_relevel("romney", "trump16", "trump20", "obama12", "clinton", "biden") %>%
        forcats::fct_drop(),
      shape = case_when(
        variable == "romney"  ~ 15,
        variable == "trump16" ~ 17,
        variable == "trump20" ~ 19,
        variable == "obama12" ~  0,
        variable == "clinton" ~  2,
        variable == "biden"   ~  1
      ),
      color = case_when(
        variable == "romney"  ~ 'chocolate1',
        variable == "trump16" ~ 'brown1',
        variable == "trump20" ~ 'darkred',
        variable == "obama12" ~ 'blue',
        variable == "clinton" ~ 'deepskyblue',
        variable == "biden"   ~ 'darkslateblue'
      ) %>% forcats::fct_relevel(
        'chocolate1',
        'brown1',
        'darkred',
        'blue',
        'deepskyblue',
        'darkslateblue'
      ),
      label = case_when(
        variable == "romney"  ~ 'Romney \'12',
        variable == "trump16" ~ 'Trump \'16',
        variable == "trump20" ~ 'Trump \'20',
        variable == "obama12" ~ 'Obama \'12',
        variable == "clinton" ~ 'Clinton \'16',
        variable == "biden"   ~ 'Biden \'20'
      ) %>% forcats::fct_relevel(
        'Romney \'12',
        'Trump \'16',
        'Trump \'20',
        'Obama \'12',
        'Clinton \'16',
        'Biden \'20'
      ) ,
      line = case_when(
        str_detect(variable, "romney|trump")         ~ 'solid',
        str_detect(variable, "obama|clinton|biden")  ~ 'longdash'
      )
    )    
}

out_summ  <- shapeify(out_summ)
out_summ2 <- shapeify(out_summ2)

colors <- levels(out_summ$color)
shapes <- c(15, 17, 19, 0, 2, 1)
lines  <- c(rep('solid', 3), rep('longdash', 3))


g1 =
  out_summ[!grepl('^N', variable) &
             as.numeric(nat_quant) != 0.85] %>%
  ggplot() +
  aes(
    nat_quant,
    value * 100,
    color = label,
    group = label,
    shape = label,
    linetype = label
  ) +
  geom_point() +
  geom_line() +
  theme_light() +
  geom_hline(yintercept = 0,
             linetype = 'dashed',
             color = 'grey') +
  labs(
    x = element_blank(),
    y = element_blank(),
    color = element_blank(),
    shape = element_blank(),
    linetype = element_blank(),
    title = '          Raw Contribution Rates'
  ) +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_linetype_manual(values = lines) +
  
  theme(text = element_text(face = 'bold', size = 10)) +
  scale_y_continuous(
    breaks = c(0:9),
    labels = scales::label_percent(scale = 1),
    position = "right"
  ) +
  theme(legend.position = 'bottom', legend.margin = margin(c(5, 5, 5, -5))) +
  theme(axis.text.x = element_text(
    angle = 25,
    vjust = .85,
    hjust = 0.5,
    size = 7
  )) +
  scale_x_discrete(labels = x_l)

g2 =
  out_summ2[!grepl('^N', variable) &
              as.numeric(nat_quant) != 0.85] %>%
  ggplot() +
  aes(
    nat_quant,
    value,
    color = variable,
    group = variable,
    shape = variable,
    linetype = variable
  ) +
  geom_point() +
  geom_line() +
  theme_light() +
  geom_hline(yintercept = 0,
             color = 'grey',
             linetype = 'dashed') +
  labs(
    x = element_blank(),
    y = element_blank(),
    color = element_blank(),
    shape = element_blank(),
    linetype = element_blank(),
    title = '          Raw Per-Capita Dollars'
  ) +
  
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_linetype_manual(values = lines) +
  theme(text = element_text(face = 'bold', size = 10)) +
  theme(legend.position = 'right', legend.margin = margin(c(5, 5, 5, -5))) +
  theme(axis.text.x = element_text(
    angle = 25,
    vjust = .85,
    hjust = 0.5,
    size = 7
  )) +
  scale_x_discrete(labels = x_l) +
  scale_y_continuous(labels = scales::label_dollar(), position = "right")

out = plot_grid(
  g1 + theme(legend.position = 'none'),
  g2 + theme(legend.position = 'none'),
  labels = str_c('(', LETTERS[1:2], ')'),
  nrow = 2
) %>%
  plot_grid(get_legend_35(
    g1 + theme(legend.margin = margin(c(-10, 0, 0, 0))) +
      guides(color = guide_legend(nrow = 1, byrow = F))
  ),
  rel_heights = c(1, .05),
  nrow = 2)

ggsave(
  filename = 'figures/fig1.pdf',
  plot = out,
  width = 8,
  height = 7,
  units = 'in'
)
